#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);

  QString versionInfo =
      QString("%2 %1 by smaller").arg(version).arg("crc校验工具");
  setWindowTitle(versionInfo);
  /* 设置icon */
  setWindowIcon(QIcon(":/img/icon64.ico"));
  checkFlag = false;
}

MainWindow::~MainWindow() { delete ui; }

void MainWindow::on_openFileBtn_clicked() {
  unsigned short size = 0;
  QString Path = QFileDialog::getOpenFileName(
      this, QString("选择bin文件"),
      QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
      QString("Bin(*.bin)"));
  if (Path.isEmpty() || Path.isEmpty()) {
    return;
  }

  filePath = Path;
  ui->linePath->setText(filePath);
  QFile file(filePath);
  QFileInfo fInfo;

  if (!file.open(QIODevice::ReadOnly)) {
    return;
  }
  fInfo.setFile(file);

  size = fInfo.size();
  QByteArray data;
  data.append((char *)&size, 2);

  ui->fileSizeLabel->setText(data.toHex().toUpper() + QString(",") +
                             QString::number(size));
}

void MainWindow::on_crcBtn_clicked() {
  qint64 len = 0;
  qint64 pos = 0;
  QString mPos;
  unsigned int crc = 0;
  do {
    if (ui->linePos->text().isEmpty() || ui->linePos->text().isNull()) {
      break;
    }
    if (ui->lineLength->text().isEmpty() || ui->lineLength->text().isNull()) {
      break;
    }
    pos = ui->linePos->text().simplified().remove(QRegExp("\\s")).toLong();
    len = ui->lineLength->text().simplified().remove(QRegExp("\\s")).toLong();

    if (pos < 0 || len <= 0) {
      break;
    }

    if (filePath.isEmpty() || filePath.isNull()) {
      break;
    }
    QFile file(filePath);
    QFileInfo fInfo;

    if (!file.open(QIODevice::ReadOnly)) {
      break;
    }
    fInfo.setFile(file);

    if (fInfo.size() < len || fInfo.size() < pos) {
      break;
    }
    if (fInfo.size() < pos + len) {
      break;
    }

    file.seek(pos);
    QByteArray data = file.read(len);

    /*  */
    ui->dataEdit->setPlainText(pFun.hex2QString(data));

    crc = CRC16((unsigned char *)data.data(), data.size());

    data.clear();
    data.append((char *)&crc, 2);

    ui->lineCrc->setText(data.toHex().toUpper());
    file.close();
  } while (0);
}

unsigned int MainWindow::CRC16(unsigned char *ptr, unsigned short len) {
  unsigned int crc = 0xffff;
  unsigned char i;
  while (len--) {
    crc ^= *ptr++;
    for (i = 0; i < 8; i++) {
      if (crc & 0x1) {
        crc >>= 1;
        crc ^= 0xa001;
      } else {
        crc >>= 1;
      }
    }
  }
  return crc;
}

void MainWindow::on_crcBtn_2_clicked() {
  QString dataString =
      ui->dataEdit->toPlainText().simplified().remove(QRegExp("\\s"));
    QByteArray sData;
  unsigned short crc = 0;
  do {
      if(dataString.isEmpty() || dataString.isNull()){
          break;
      }
      sData = pFun.QString2Hex(dataString);
      crc = CRC16((unsigned char *)sData.data(),sData.length());

      sData.clear();
      sData.append((char *)&crc, 2);

      ui->dataCrcEdit->setText(sData.toHex().toUpper());
  } while (0);
}

void MainWindow::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked()){
        checkFlag = true;
    }else{
        checkFlag = false;
    }
}

void MainWindow::on_lineEdit_editingFinished()
{
    QString numS = ui->lineEdit->text().simplified().remove(QRegExp("\\s"));
    qint64 num = numS.toLong();
    QString tmp;

    do{
        if(num<=0) break;
        for(qint64 i = 0;i<num;i++){
            tmp+=QString("00 ");
        }
        ui->dataEdit->moveCursor(QTextCursor::End);
        ui->dataEdit->insertPlainText(tmp);
    }while(0);
}

